package com.budwk.app.hlw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.hlw.models.Hlw_config;
import com.budwk.app.hlw.services.HlwConfigService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/hlw/config")
@ApiDefinition(tag = "互联网配置")
@Slf4j
public class HlwConfigController{
    @Inject
    private HlwConfigService hlwConfigService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询互联网配置")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = Hlw_config.class
    )
    @ApiResponses(
    implementation = Hlw_config.class
    )
    @SaCheckPermission("hlw.config")
    public Result<?> list(@Param("..")Hlw_config bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getAppname() !=null && Strings.isNotBlank(bean.getAppname().toString())) {
            cnd.and("appname","=",bean.getAppname());
        }
        
        if (bean.getGhid() !=null && Strings.isNotBlank(bean.getGhid().toString())) {
            cnd.and("ghid","=",bean.getGhid());
        }
        
        if (bean.getAppid() !=null && Strings.isNotBlank(bean.getAppid().toString())) {
            cnd.and("appid","=",bean.getAppid());
        }
        
        if (bean.getAppsecret() !=null && Strings.isNotBlank(bean.getAppsecret().toString())) {
            cnd.and("appsecret","=",bean.getAppsecret());
        }
        
        if (bean.getSys_unit_id() !=null && Strings.isNotBlank(bean.getSys_unit_id().toString())) {
            cnd.and("sys_unit_id","=",bean.getSys_unit_id());
        }
        
        if (bean.getEncodingAESKey() !=null && Strings.isNotBlank(bean.getEncodingAESKey().toString())) {
            cnd.and("encodingAESKey","=",bean.getEncodingAESKey());
        }
        
        if (bean.getToken() !=null && Strings.isNotBlank(bean.getToken().toString())) {
            cnd.and("token","=",bean.getToken());
        }
        
        if (bean.getAccess_token() !=null && Strings.isNotBlank(bean.getAccess_token().toString())) {
            cnd.and("access_token","=",bean.getAccess_token());
        }
        
        if (bean.getAccess_token_expires() !=null && Strings.isNotBlank(bean.getAccess_token_expires().toString())) {
            cnd.and("access_token_expires","=",bean.getAccess_token_expires());
        }
        
        if (bean.getAccess_token_lastat() !=null && Strings.isNotBlank(bean.getAccess_token_lastat().toString())) {
            cnd.and("access_token_lastat","=",bean.getAccess_token_lastat());
        }
        
        if (bean.getMchid() !=null && Strings.isNotBlank(bean.getMchid().toString())) {
            cnd.and("mchid","=",bean.getMchid());
        }
        
        return Result.data(hlwConfigService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增互联网配置")
    @ApiFormParams(
        implementation = Hlw_config.class
    )
    @ApiResponses
    @SLog(tag = "新增互联网配置", value = "${args[0]}")
    @SaCheckPermission("hlw.config.create")
    public Result<?> create(@Param("..")Hlw_config hlwConfig, HttpServletRequest req) {
        hlwConfig.setCreatedBy(SecurityUtil.getUserId());
        hlwConfigService.insert(hlwConfig);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改互联网配置")
    @ApiFormParams(
        implementation = Hlw_config.class
    )
    @ApiResponses
    @SLog(tag = "修改互联网配置", value = "${args[0]}")
    @SaCheckPermission("hlw.config.update")
    public Result<?> update(@Param("..")Hlw_config hlwConfig, HttpServletRequest req) {
        hlwConfig.setUpdatedBy(SecurityUtil.getUserId());
        hlwConfigService.updateIgnoreNull(hlwConfig);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取互联网配置")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("hlw.config")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        Hlw_config hlwConfig = hlwConfigService.fetch(id);
        if (hlwConfig == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(hlwConfig);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除互联网配置")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除互联网配置", value = "${args[0]}")
    @SaCheckPermission("hlw.config.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        Hlw_config hlwConfig = hlwConfigService.fetch(id);
        if (hlwConfig == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        hlwConfigService.delete(id);
        req.setAttribute("_slog_msg", hlwConfig.getId());
        return Result.success();
    }
}
